﻿// 2023/3/20: 首个版本
// 2023/9/25: 改为直接输入Icon对象
// 2024/6/25: 改为异步

using System;
using System.Threading.Tasks;
using ASEva;
using ASEva.Samples;
using ASEva.UIEto;
using Eto.Drawing;

namespace CommonSource
{
    class SignalChooserSwitch
    {
        public static async Task<SignalConfig> Select(SignalConfig origin, bool withScale, bool withSignBit, string unit, Icon icon = null)
        {
            if (origin == null) origin = new SignalConfig();
            if (withScale)
            {
                if (origin.Scale == null) origin.Scale = 1;
            }
            else
            {
                if (origin.Scale != null) origin.Scale = null;
            }

            if (withSignBit)
            {
                var dialog = new SignedSignalChooser(origin.SignalID, origin.SignBitSignalID, origin.Scale, unit, icon);
                await App.RunDialog(dialog);

                if (dialog.Result == SignedSignalChooser.ResultType.Delete) return null;
                else if (dialog.Result == SignedSignalChooser.ResultType.Select) return dialog.SelectedSignal;
                else return origin;
            }
            else
            {
                var dialog = new SignalChooser(origin.SignalID, origin.Scale, unit, icon);
                await App.RunDialog(dialog);

                if (dialog.Result == SignalChooser.ResultType.Delete) return null;
                else if (dialog.Result == SignalChooser.ResultType.Select) return dialog.SelectedSignal;
                else return origin;
            }
        }
    }
}
